package com.itany.ems.dao.impl;

import com.itany.ems.dao.EmpDao;
import com.itany.ems.entity.Emp;
import com.itany.ems.exception.DataAccessException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/3 13:47
 * Version:1.0-SNAPSHOT
 */
public class EmpDaoImpl implements EmpDao {

    public static final String JDBC_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
    public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8";
    public static final String JDBC_USERNAME = "root";
    public static final String JDBC_PASSWORD = "";

    @Override
    public void insertEmp(Emp emp) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn = DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);

            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append("   (name,salary,gender,edu,hobbies)")
                    .append(" values ")
                    .append("   (?,?,?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setDouble(2,emp.getSalary());
            ps.setString(3,emp.getGender());
            ps.setString(4,emp.getEdu());
            // 对于hobbies属性,在实体类中定义的是一个数组类型
            // 而在数据库中是一个varchar类型
            // 因此,我们需要将对应中的数组转换成字符串传递到数据库中
            // 且后期通过数据库查询的hobbies传递到页面中时需要将字符串转换成数组
            // 因此,可以定义一个工具方法,进行转换
            ps.setString(5,emp.getHob());

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
            throw new DataAccessException("数据访问异常");
        }
    }

    @Override
    public List<Emp> selectAll() throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Emp> emps = new ArrayList<>();
        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn = DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Emp emp = new Emp();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setSalary(rs.getDouble("salary"));
                emps.add(emp);
            }
            return emps;
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataAccessException("数据访问异常");
        }
    }

    @Override
    public Emp selectById(Integer id) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Emp emp = null;
        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn = DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                emp = new Emp();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setSalary(rs.getDouble("salary"));
                emp.setGender(rs.getString("gender"));
                emp.setEdu(rs.getString("edu"));
                emp.setHob(rs.getString("hobbies"));
            }
            return emp;
        } catch (Exception e) {
            throw new DataAccessException("数据访问异常");
        }
    }

    @Override
    public void updateEmp(Emp emp) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn = DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql = new StringBuffer()
                    .append(" update t_emp ")
                    .append(" set ")
                    .append("   name = ?,")
                    .append("   salary = ?,")
                    .append("   gender = ?,")
                    .append("   edu = ?,")
                    .append("   hobbies = ?")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setDouble(2,emp.getSalary());
            ps.setString(3,emp.getGender());
            ps.setString(4,emp.getEdu());
            ps.setString(5,emp.getHob());
            ps.setInt(6,emp.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new DataAccessException("数据访问异常");
        }
    }

    @Override
    public void deleteById(Integer id) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn = DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql = new StringBuffer()
                    .append(" delete from t_emp ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new DataAccessException("数据访问异常");
        }
    }
}
